//## 거래처 정보 저장 ####################
function fnSaveCompany()
{
	if (!$("#company").val())
	{
		alert("거래처를 바르게 입력해 주세요!");
		$("#company").focus();
		return;
	}
	if (!$("#phone").val())
	{
		alert("연락처를 바르게 입력해 주세요!");
		$("#phone").focus();
		return;
	}
	var param  = "company="+encodeURIComponent($("#company").val());
		param += "&owner="+$("#owner").val();
		param += "&phone="+$("#phone").val();
		param += "&fax="+$("#fax").val();
		param += "&major="+encodeURIComponent($("#major").val());
		param += "&compType="+encodeURIComponent($("#compType").val());
		param += "&compPart="+encodeURIComponent($("#compPart").val());
		param += "&cellPhone="+$("#cellPhone").val();
		param += "&compNo="+$("#compNo").val();
		param += "&memo="+encodeURIComponent($("#memo").val());
		param += "&flag="+$("#flag").val();
		param += "&addr="+encodeURIComponent($("#addr").val());
		param += "&compCd="+$("#compCd").val();

	$.ajax({
		url: '/action/company/save.asp',
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 거래처 정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	clstTable("Grid01");
	$("#tr01Result").show();
	setNullRow("loadSpan","1");
	var valUrl = "/action/company/list.asp";
	var param = "findCompany="+encodeURIComponent($("#findCompany").val());
		param += "&findMajor="+encodeURIComponent($("#findMajor").val());

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
			fnCompanyReset();
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	clstTable("Grid01");

	$("#tr01Result").css("display","none");
	$("#loadSpan1").html("");
	var nodeCnt = $(G_grid01Rslt).find("Companys").find("CompList").length;

	if(nodeCnt == 0)
	{
		$("#tr01Result").css("display","");
		$("#loadSpan1").html("검색된 자료가 없습니다.");
		$("#grid01Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var compCd			= $(G_grid01Rslt).find("Companys").find("CompList").eq(j).find("compCd").text();
		var company			= $(G_grid01Rslt).find("Companys").find("CompList").eq(j).find("company").text();
		var phone			= $(G_grid01Rslt).find("Companys").find("CompList").eq(j).find("phone").text();
		var major			= $(G_grid01Rslt).find("Companys").find("CompList").eq(j).find("major").text();
		var cellPhone		= $(G_grid01Rslt).find("Companys").find("CompList").eq(j).find("cellPhone").text();		
		var flag			= $(G_grid01Rslt).find("Companys").find("CompList").eq(j).find("flag").text();

		ji = j+1;
		
		var showFlag = "";
		if(flag == "Y") showFlag = "거래중";
		else showFlag = "정 지";
		
		if (flag == "N") var bgColor = "#F2F2F2";
		else var bgColor = "#FFFFFF";

		grid01Array[0] = ji;
		grid01Array[1] = company;
		grid01Array[2] = phone;
		grid01Array[3] = major+"&nbsp;";
		grid01Array[4] = cellPhone+"&nbsp;";
		grid01Array[5] = showFlag;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid01", grid01Array, bgColor, ",left,,,,,", "fnSetCompany('"+compCd+"')", boldLine);
	}
}


//## 거래처 상세정보 표시 ####################
function fnSetCompany(compCd)
{
	var valUrl = "/action/company/view.asp";
	var param = "compCd="+compCd;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			$("#compCd").val($(xml).find("compCd").text());
			$("#company").val($(xml).find("company").text());
			$("#owner").val($(xml).find("owner").text());
			$("#phone").val($(xml).find("phone").text());
			$("#fax").val($(xml).find("fax").text());
			$("#major").val($(xml).find("major").text());
			$("#cellPhone").val($(xml).find("cellPhone").text());
			$("#compNo").val($(xml).find("compNo").text());
			$("#compPart").val($(xml).find("compPart").text());
			$("#compType").val($(xml).find("compType").text());
			$("#memo").val($(xml).find("memo").text());
			$("#flag").val($(xml).find("flag").text());
			$("#addr").val($(xml).find("addr").text());
			if ($(xml).find("updateDt").text())
			{
				$("#accessInfo").val("최종수정 : "+$(xml).find("issueNm").text()+" / "+$(xml).find("updateDt").text());
			}
			else
			{
				$("#accessInfo").val("최종수정 : "+$(xml).find("issueNm").text()+" / "+$(xml).find("issueDt").text());
			}

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 거래처 정보 Reset 기능
function fnCompanyReset()
{
	$("#compCd").val("");
	$("#company").val("");
	$("#owner").val("");
	$("#phone").val("");
	$("#fax").val("");
	$("#major").val("");
	$("#cellPhone").val("");
	$("#compNo").val("");
	$("#compPart").val("");
	$("#compType").val("");
	$("#memo").val("");
	$("#addr").val("");
	$("#flag").val("Y");
	$("#accessInfo").val("");
}


//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findCompany").val("");
	$("#findMajor").val("");
}